layui.define(['layer' ,'element', 'form','jquery' ],function(exports) {
    var layer = layui.layer //弹层

        ,element = layui.element //元素操作
        ,$ = layui.jquery

        ,form = layui.form;

        var userInfo = null;




    sysSetPassword = {


        init_form:function (){
            $.ajax({
                type: "GET",
                url: "/system/user/nowUserInfo" ,
                success:function(resp){
                    var code = resp.code;
                    userInfo = resp.data;
                    if (code === 200 ){
                        form.val("dataForm",{
                            "id" : userInfo.userid
                        })
                    }else {
                        layer.msg("获取个人信息失败 " + resp.msg, {icon: 5});
                    }
                }
            });
        },

        submit_form:function () {
            form.on('submit(setmypass)', function(data) {//REST风格 function(data) 因为submit监听了submitTable表单，data代表的是表单中的整体对象
                var old_password= data.field.old_password;
                var new_password= data.field.new_password;
                var new_confirm_password= data.field.new_confirm_password;
                if (new_confirm_password !== new_password) {
                    layer.msg('新密码之间不相同，请确认后提交！', {icon: 5});
                }else {
                    $.ajax({
                        type: "PUT",
                        contentType:"application/x-www-form-urlencoded",//设置请求参数类型为json字符串
                        url: "/system/user/newPassword"+"/"+userInfo.userid,
                        data: {
                            oldPassword: old_password,
                            newPassword: new_password,
                            newConfirmPassword: new_confirm_password
                        },
                        success:function(resp){
                            var code = resp.code;
                            if (code === 200 ){

                                layer.msg('修改成功', {icon: 1});

                            }else {
                                layer.msg("修改失败 " + resp.msg, {icon: 5});
                            }
                        }
                    });
                    return false;
                }

            })
        }



    };

    sysSetPassword.init_form();
    sysSetPassword.submit_form();




    exports("sysSetPassword",sysSetPassword);



});